package com.prim.threadcoreknowledge.stopthread;

import java.util.Arrays;

/**
 * 如果while里面方try catch会导致中断失效
 * 线程会正常运行到结束
 *
 * @author prim
 */
public class CantInterrupt {
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            int num = 0;
            while (num <= 10000 && !Thread.currentThread().isInterrupted()) {
                if (num % 100 == 0) {
                    System.out.println(num + "是100的倍数");
                }
                num++;
                //如果线程收到中断
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("任务执行完毕");
        };
        Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(5000);
        thread.interrupt();//interrupt只是通知了线程中断，但是线程是否要中断取决于线程本身。
    }
}
